package git.soulbgm.utils;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Object工具类
 *
 * @author SoulBGM
 * @version 1.0
 * @date 2019/10/14 10:06
 */
public class ObjectUtil {

    private final static String FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 字符串转各种类型
     *
     * @param str   字符串
     * @param clazz 类型的class
     * @return 符合clazz的对象
     */
    public static Object strToObj(String str, Class clazz) {
        if (clazz == null || str == null) {
            return null;
        }
        if (clazz == byte.class || clazz == Byte.class) {
            return Byte.parseByte(str);
        } else if (clazz == short.class || clazz == Short.class) {
            return Short.parseShort(str);
        } else if (clazz == int.class || clazz == Integer.class) {
            return Integer.parseInt(str);
        } else if (clazz == long.class || clazz == Long.class) {
            return Long.parseLong(str);
        } else if (clazz == float.class || clazz == Float.class) {
            return Float.parseFloat(str);
        } else if (clazz == double.class || clazz == Double.class) {
            return Double.parseDouble(str);
        } else if (clazz == boolean.class || clazz == Boolean.class) {
            return Boolean.parseBoolean(str);
        } else if (clazz == char.class || clazz == Character.class) {
            return str.charAt(0);
        } else if (clazz == char[].class || clazz == Character[].class) {
            return str.toCharArray();
        } else if (clazz == Date.class) {
            SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
            try {
                return sdf.parse(str);
            } catch (ParseException e) {
                return null;
            }
        }
        return str;
    }

    /**
     * 实体对象转LinkedHashMap对象 为null也添加
     *
     * @param model 实体对象
     * @return LinkedHashMap对象
     */
    public static Map<String, Object> modelToMap(Object model) {
        return modelToMap(model, true);
    }

    /**
     * 实体对象转LinkedHashMap对象
     *
     * @param model  实体对象
     * @param isNull 是否添加值为null的字段 true添加 false不添加
     * @return LinkedHashMap对象
     */
    public static Map<String, Object> modelToMap(Object model, boolean isNull) {
        if (model == null) {
            return null;
        }
        Class<?> modelClass = model.getClass();
        Field[] fields = modelClass.getDeclaredFields();
        Map<String, Object> map = new LinkedHashMap<>(fields.length);
        for (Field field : fields) {
            Object value = ClassUtil.executeGetMethod(model, field);
            boolean flag = (!isNull && value != null) || isNull;
            if (flag) {
                map.put(field.getName(), value);
            }
        }
        return map;
    }

    /**
     * 获取某个model对象的值并将所有值组合成一个List
     *
     * @param modelList 实体对象列表
     * @param fieldName 字段名称
     * @param tClass    需要获取字段的类型
     * @return 组合的List
     */
    public static <T> List<T> getFieldList(List<?> modelList, String fieldName, Class<T> tClass) throws NoSuchFieldException {
        if (modelList == null) {
            return null;
        }
        List<T> dataList = new ArrayList<>(modelList.size());
        for (Object model : modelList) {
            Class<?> modelClass = model.getClass();
            Field field = modelClass.getDeclaredField(fieldName);
            Object val = ClassUtil.executeGetMethod(model, field);
            dataList.add((T) val);
        }
        return dataList;
    }

    /**
     * 获取某个model对象的值并将所有值以,组合成一个字符串
     *
     * @param modelList 实体对象列表
     * @param fieldName 字段名称
     * @return 组合之后的字符串
     */
    public static String getFieldStr(List<?> modelList, String fieldName) throws NoSuchFieldException {
        return getFieldStr(modelList, fieldName, ",");
    }

    /**
     * 获取某个model对象的值并将所有值组合成一个字符串
     *
     * @param modelList 实体对象列表
     * @param fieldName 字段名称
     * @param linkStr   字符串与字符串连接之间的字符
     * @return 组合之后的字符串
     */
    public static String getFieldStr(List<?> modelList, String fieldName, String linkStr) throws NoSuchFieldException {
        if (modelList == null) {
            return null;
        }
        String[] array = new String[modelList.size()];
        for (int i = 0; i < modelList.size(); i++) {
            Object model = modelList.get(i);
            Class<?> modelClass = model.getClass();
            Field field = modelClass.getDeclaredField(fieldName);
            Object val = ClassUtil.executeGetMethod(model, field);
            array[i] = val.toString();
        }
        return StringUtil.join(linkStr, array);
    }


}
